<?php

namespace app\common\model;

use app\common\Base;
use think\Model;

class Comment extends Model
{
    //引入特性
    use Base;

    //关联答案表
    public function comToAns()
    {
        return $this->belongsTo('Answer','aid');
    }

    //关联用户表
    public function comToUser()
    {
        return $this->belongsTo('User','uid');
    }

    //查询某个答案的评论
    public function getCom($aid,$arr)
    {
        $limit = $arr['limit'] ?? config('api.limit');
        $limit = min(config('api.maxlist'),$limit);
        $re = $this->where('aid',$aid)->order('c_zan','desc')->limit($limit)->selectOrFail();
        //关联用户
        foreach ($re as $v){
            $v['user'] = $v->comToUser->user;
            $v['userimg'] = $v->comToUser->pic;
        }
        return $re;
    }

    //写入一个评论
    public function saveCom($arr)
    {
        //验证器验证

        //写入
        return $this->create($arr) ? $this->create($arr) : [];

    }

    //获取某个用户的评论
    public function getUserCom($uid,$arr)
    {
        $rows = pageque($this,$arr,['uid'=>$uid]);
        foreach($rows as $v){
            /*$v['user'] = $v->comToUser->user;
            $v['userimg'] = $v->comToUser->pic;*/
            $v['ans'] = $v->comToAns->answer;
        }
        return $rows;
    }

}
